package com.team7.happycommunity.propertymanagement.service.impl;

import com.team7.happycommunity.propertymanagement.dao.PpPayserviceMapper;
import com.team7.happycommunity.propertymanagement.dao.PropertyImgMapper;
import com.team7.happycommunity.propertymanagement.dao.PropertyManageMapper;
import com.team7.happycommunity.propertymanagement.dao.ServiceImgMapper;
import com.team7.happycommunity.propertymanagement.dto.PropertyserviceForSave;
import com.team7.happycommunity.propertymanagement.pojo.PpPayservice;
import com.team7.happycommunity.propertymanagement.pojo.PropertyManage;
import com.team7.happycommunity.propertymanagement.service.PropertyServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class PropertyServiceServiceImpl implements PropertyServiceService {

    @Autowired
    PpPayserviceMapper ppPayserviceMapper;

    @Autowired
    PropertyManageMapper propertyManageMapper;

    @Autowired
    ServiceImgMapper serviceImgMapper;

    @Override
    public boolean save(PropertyserviceForSave propertyserviceForSave) {
        PropertyManage propertyManage = propertyManageMapper.selectByUserName(propertyserviceForSave.getUsername());
        PpPayservice ppPayservice = new PpPayservice();
        ppPayservice.setPayname(propertyserviceForSave.getPayname());
        ppPayservice.setPayprice(propertyserviceForSave.getPayprice());
        ppPayservice.setPaytime(propertyserviceForSave.getPaytime());
        ppPayservice.setPropertyid(propertyManage.getId());
        ppPayservice.setPayway(propertyserviceForSave.getPayway());
        ppPayservice.setServicecontext(propertyserviceForSave.getServicecontext());
        // 判断新增或修改
        Integer id = propertyserviceForSave.getId();
        if(id == null){
            ppPayservice.setServiceType(1);
            ppPayservice.setStatus(0);
            int result = ppPayserviceMapper.insertSelective(ppPayservice);
            if(ppPayservice.getId() == null){
                return false;
            }
            // 插入图片信息
            List<String> ls = Arrays.asList(propertyserviceForSave.getPath().split(";"));
            if(ls.size() > 0){
                int i = serviceImgMapper.insertBatch(ls,ppPayservice.getId());
            }
        }else{
            ppPayservice.setId(id);
            int result = ppPayserviceMapper.updateByPrimaryKeySelective(ppPayservice);
            if(result == 0){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<PpPayservice> getListByPageAndUsername(Integer currentPage, Integer pageSize, String username) {
//        ppPayserviceMapper.selectByPageAndUsername(currentPage,pageSize,username);
        PropertyManage propertyManage = propertyManageMapper.selectByUserName(username);
        return ppPayserviceMapper.selectByPageAndUsername(currentPage,pageSize,propertyManage.getId());
    }

    @Override
    public int delete(Integer id) {
        int result = ppPayserviceMapper.deleteByPrimaryKey(id);
        return result;
    }
}
